#include "ui_local.h"
#include "g_local.h"

using namespace irr;
using namespace gui;
using namespace video;

CVar ui_drawFPS("ui_drawFPS", "1", CVAR_ARCHIVE|CVAR_BOOL, "Draws FPS (frames per second)");
og::Timer fpsTimer;
static unsigned int fpsFrameCount = 0;
static float currentFPS = 0;

/*
===========================
UI_Main()
Inits all of the various UI objects and reads the UI files
===========================
*/

void UI_Main()
{
	gi->Print("Initializing UI..\n");

	// Force the splash screen to come up
	IImage *test = r->GetImageResource("menu/splash.jpg");
	gi->Print("Test\n");

	fpsTimer.Start();
}

/*
===========================
UI_DrawFPS()
Draw the FPS, if ui_drawFPS is 1
===========================
*/
void UI_DrawFPS()
{

	if(fpsFrameCount < 200)
	{
		fpsFrameCount++;
	}
	else
	{
		fpsTimer.Stop();
		currentFPS = static_cast<float>(fpsFrameCount * 1000.0/static_cast<double>(fpsTimer.MilliSeconds()));
		fpsFrameCount = 0;
		fpsTimer.Start();
	}

	// ...aanyway. Let's render the FPS, shall we?
	// The reason I'm not using the Irrlicht one is simple: it's rather slow and unwieldy. Doesn't update nearly as fast as I would like.
	// So first thing we should probably do is integrate the font system into the file system. Not quite as easy as it sounds apparently...
	//r->GetFontResource("fonts/fontlucida.png")->draw(core::stringw(L"Durr hurr text heer"), core::rect<s32>(10, 10, 100, 20), video::SColor(255, 0, 0, 0));
}

/*
===========================
UI_Draw2D()
Draws all elements that are NOT related to GUI/UI
===========================
*/
void UI_Draw2D()
{
	if(ui_drawFPS.GetBool())
	{
		UI_DrawFPS();
	}
}

/*
===========================
UI_Frame()
Does all of the stuff which should be performed on a frame basis
===========================
*/

void UI_Frame()
{
	UI_Draw2D();
}

/*
===========================
GameClass::GUIEvent()
Stuff sent by the renderer (Irrlicht) to the engine (BaseEngine)
which gets redirected to here.
===========================
*/

void GameClass::GUIEvent(int eventType, void *caller, void *element)
{
	IGUIElement *callerElement = (IGUIElement *)caller;
	IGUIElement *sideElement = (IGUIElement *)element;
}

